package com.fita.entity.order.reverse.aviation;

import com.demo2do.core.utils.JsonUtils;
import com.fita.entity.order.reverse.cruises.CruisesRequest;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
import org.hibernate.type.descriptor.java.MutableMutabilityPlan;
import org.hibernate.type.descriptor.sql.LongVarcharTypeDescriptor;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 11200
 * @date: 2019/8/30
 */

/**
 *
 */
@SuppressWarnings({"serial", "rawtypes"})
public class CommaDelimitedCruisesUserInfoType extends AbstractSingleColumnStandardBasicType<List> {

    /**
     *
     */
    private static final long serialVersionUID = 1L;


    public CommaDelimitedCruisesUserInfoType() {
        super(
                LongVarcharTypeDescriptor.INSTANCE,
                new CommaDelimitedCruisesUserInfoJavaTypeDescriptor()
        );
    }

    @Override
    public String getName() {
        return "comma_delimited_cruises_user_info";
    }


    public static class CommaDelimitedCruisesUserInfoJavaTypeDescriptor extends AbstractTypeDescriptor<List> {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public CommaDelimitedCruisesUserInfoJavaTypeDescriptor() {
            super(
                    List.class,
                    new MutableMutabilityPlan<List>() {
                        @SuppressWarnings("unchecked")
                        @Override
                        protected List deepCopyNotNull(List value) {
                            return new ArrayList( value );
                        }
                    }
            );
        }

        @Override
        public String toString(List value) {
            return JsonUtils.toJson(value);
        }

        @Override
        public List fromString(String string) {
            if (StringUtils.isBlank(string)){
                return new ArrayList();
            }
            return JsonUtils.parseAsList(string, CruisesRequest.CruisesUserInfo.class);
        }

        /**
         * Unwrap an instance of our handled Java type into the requested type.
         * <p/>
         * As an example, if this is a {@code JavaTypeDescriptor<Integer>} and we are asked to unwrap
         * the {@code Integer value} as a {@code Long} we would return something like
         * <code>Long.valueOf( value.longValue() )</code>.
         * <p/>
         * Intended use is during {@link PreparedStatement} binding.
         *
         * @param value   The value to unwrap
         * @param type    The type as which to unwrap
         * @param options The options
         * @return The unwrapped value.
         */
        @SuppressWarnings("unchecked")
        @Override
        public <X> X unwrap(List value, Class<X> type, WrapperOptions options) {
            return (X) toString(value);
        }

        /**
         * Wrap a value as our handled Java type.
         * <p/>
         * Intended use is during {@link ResultSet} extraction.
         *
         * @param value   The value to wrap.
         * @param options The options
         * @return The wrapped value.
         */
        @Override
        public <X> List wrap(X value, WrapperOptions options) {
            return fromString((String) value);
        }
    }


}